package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.ItemModelDao;
import mil.af.amc.adpetracker.model.EquipmentItem;
import mil.af.amc.adpetracker.model.EquipmentType;
import mil.af.amc.adpetracker.model.ItemModel;
import mil.af.amc.adpetracker.model.Manufacturer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Hibernate implementation of the ItemModelDao
 *
 * @author TSgt Jason Ferguson
 */

@SuppressWarnings({"UnnecessaryLocalVariable", "unchecked"})
@Service("itemModelDao")
public class HibernateItemModelDao extends HibernateGenericDao<ItemModel> implements ItemModelDao {

    Log log = LogFactory.getLog(HibernateItemModelDao.class);

    public HibernateItemModelDao() {
        super(ItemModel.class);
    }

    public List<ItemModel> getAllByManufacturer(Manufacturer manu) {

        DetachedCriteria crit = DetachedCriteria.forClass(ItemModel.class);
        DetachedCriteria manufacturerCrit = crit.createCriteria("manufacturer");
        manufacturerCrit.add(Restrictions.ilike("name", manu.getName()));

        crit.addOrder(Order.asc("name"));

        List<ItemModel> resultList = null;
        try {
            resultList = (List<ItemModel>) getHibernateTemplate().findByCriteria(crit);
        } catch (DataAccessException e) {
            log.debug(e);
        }

        return resultList;

    }

    public List<ItemModel> getItemModelsByEquipmentType(final EquipmentType equipmentType) {

        DetachedCriteria crit = DetachedCriteria.forClass(EquipmentItem.class);
        // narrow down to the model
        DetachedCriteria etCrit = crit.createCriteria("itemModel");

        // narrow down to the type
        DetachedCriteria subEtCrit = etCrit.createCriteria("type");

        subEtCrit.add(Restrictions.ilike("name", equipmentType.getName()));

        List<EquipmentItem> equipmentItemResults = getHibernateTemplate().findByCriteria(crit);
        List<ItemModel> results = new ArrayList<ItemModel>();

        for (Iterator<EquipmentItem> it = equipmentItemResults.iterator(); it.hasNext();) {
            EquipmentItem item = it.next();
            if (!results.contains(item.getItemModel())) {
                results.add(item.getItemModel());
            }
        }
        return results;
    }

    public List<ItemModel> getAllByItemTypeAndManufacturer(EquipmentType type, Manufacturer manu) {

        DetachedCriteria crit = DetachedCriteria.forClass(ItemModel.class);
        DetachedCriteria typeCrit = crit.createCriteria("type");
        typeCrit.add(Restrictions.eq("id", type.getId()));
        DetachedCriteria manufacturerCrit = crit.createCriteria("manufacturer");
        manufacturerCrit.add(Restrictions.eq("id", manu.getId()));

        return (List<ItemModel>) getHibernateTemplate().findByCriteria(crit);
    }
}
